package com.example.as_sum.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.as_sum.BuildConfig;
import com.example.as_sum.R;
import com.example.as_sum.adapter.MainPagerAdapter;
import com.example.as_sum.application.AppApplication;
import com.example.as_sum.application.Constant;
import com.example.as_sum.fragment.MainFirstFragment;
import com.example.as_sum.fragment.MainFourFragment;
import com.example.as_sum.fragment.MainThreeFragment;
import com.example.as_sum.fragment.MainTwoFragment;
import com.example.as_sum.util.Log;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends BaseActivity implements View.OnClickListener {
    private final String Tga=MainActivity.this.getClass().getSimpleName();
    private MainActivityReceiver receiver;
    private ViewPager mViewPager;
    private DrawerLayout mDrawerLayout;
    private LinearLayout mLinearLayout;
    private TextView tv_title1,tv_title2,tv_title3,tv_title4,bg_move;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        registerReceiver();
        InitImageview();
        Log.e(Tga, "onCreate");
        boolean r= BuildConfig.LOG_DEBUG;
        String baseUrl=getResources().getString(R.string.base_url);
        if(r){
             Log.e(Tga, "log_show:true，地址"+baseUrl);
        }
        else
            Log.e(Tga, "log_show:false");
    }
    public void toggleSlideState(View view){
        if(mDrawerLayout.isDrawerOpen(mLinearLayout)){
            mDrawerLayout.closeDrawer(mLinearLayout);
        }else{
            mDrawerLayout.openDrawer(mLinearLayout);
        }
    }

    @Override
    public int getContentId() {
        return R.layout.activity_main;
    }
    private List<Fragment> mFragments;
    @Override
    public void initView() {
        mLinearLayout=(LinearLayout)this.findViewById(R.id.main_contentlayout);
        mDrawerLayout=(DrawerLayout)this.findViewById(R.id.main_drawerLayout);
        bg_move=(TextView)this.findViewById(R.id.bg_move);
        tv_title1=(TextView)this.findViewById(R.id.tv_title1);
        tv_title2=(TextView)this.findViewById(R.id.tv_title2);
        tv_title3=(TextView)this.findViewById(R.id.tv_title3);
        tv_title4=(TextView)this.findViewById(R.id.tv_title4);

        mFragments=new ArrayList<>();
        mViewPager=(ViewPager)this.findViewById(R.id.viewPager);
        Fragment fragment1= MainFirstFragment.newInstance();
        Fragment fragment2= MainTwoFragment.newInstance();
        Fragment fragment3= MainThreeFragment.newInstance();
        Fragment fragment4= MainFourFragment.newInstance();
        mFragments.add(fragment1);
        mFragments.add(fragment2);
        mFragments.add(fragment3);
        mFragments.add(fragment4);
        MainPagerAdapter adapter=new MainPagerAdapter(getSupportFragmentManager(),mFragments);
        mViewPager.setAdapter(adapter);
        mViewPager.setCurrentItem(0);
        mViewPager.setOnPageChangeListener(new PagerListener());
    }

    @Override
    public void addListener() {
        tv_title1.setOnClickListener(this);
        tv_title2.setOnClickListener(this);
        tv_title3.setOnClickListener(this);
        tv_title4.setOnClickListener(this);
    }

    @Override
    public void loadData() {

    }

    @Override
    public Handler getHandler() {
        return null;
    }
    @Override
    public void onBackPressed() {
        if(AppApplication.aContext.isExitApp()) {
            super.onBackPressed();
        }
    }

    @Override
    protected void onDestroy() {
        unregisterReceiver(receiver);
        super.onDestroy();
        Log.e(Tga, "onDestroy");
    }

    public void registerReceiver() {
        receiver = new MainActivityReceiver();
        IntentFilter filter = new IntentFilter();
        filter.addAction(Constant.ACTION_LOGOUT);
        filter.addAction(Constant.ACTION_LOGIN_SC);
        registerReceiver(receiver, filter);

    }

    @Override
    public void onClick(View v) {
        tv_title1.setTextColor(Color.parseColor("#000000"));
        tv_title2.setTextColor(Color.parseColor("#000000"));
        tv_title3.setTextColor(Color.parseColor("#000000"));
        tv_title4.setTextColor(Color.parseColor("#000000"));
        switch (v.getId()){
            case R.id.tv_title1:
                tv_title1.setTextColor(Color.parseColor("#3F51B5"));
                mViewPager.setCurrentItem(0);
                break;
            case R.id.tv_title2:
                tv_title2.setTextColor(Color.parseColor("#3F51B5"));
                mViewPager.setCurrentItem(1);
                break;
            case R.id.tv_title3:
                tv_title3.setTextColor(Color.parseColor("#3F51B5"));
                mViewPager.setCurrentItem(2);
                break;
            case R.id.tv_title4:
                tv_title4.setTextColor(Color.parseColor("#3F51B5"));
                mViewPager.setCurrentItem(3);
                break;
        }
    }

    class MainActivityReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if(Constant.ACTION_LOGOUT.equals(action)){
                Log.e("------>", "接收广播,安全退出");
                AppApplication.aContext.gotoLogin(MainActivity.this, true);
            }else if(Constant.ACTION_LOGIN_SC.equals(action)){
                Log.e("------>","接收广播,登陆成功");
            }
        }
    }
    /**
     * 图片长度
     */
    private int pic_length;
    /**
     * 当前位置的偏移地址
     */
    private int offset;
    /**
     * 当前每一个菜单的长度
     */
    private int screenW;
    /**
     * 记录当前到屏幕左边的偏移量
     */
    private int nowleft;
    private void InitImageview() {
        // TODO Auto-generated method stub

        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        screenW = dm.widthPixels;// 获取分辨率宽度 ,就是屏幕的宽度
        // 吧屏幕的宽度除以4，平均分成了四分，在设置它的下划线居中
        screenW = screenW / 4;
        pic_length=screenW/4*3;//当前菜单的3/4
        //pic_length = screenW;
        nowleft = offset;// 初始一下默认imageview的初始位置距离左边屏幕的距离
        offset = (screenW - pic_length) / 2;// 计算偏移量
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(pic_length,6);
        params.leftMargin = offset;
        bg_move.setLayoutParams(params);
    }
    class PagerListener implements ViewPager.OnPageChangeListener {

        @Override
        public void onPageScrollStateChanged(int arg0) {
            // TODO Auto-generated method stub
            // 其中arg0这个参数 .此方法是在状态改变的时候调用,
            // 有三种状态（0，1，2）。arg0
            // ==1的时辰默示正在滑动，arg0==2的时辰默示滑动完毕了，arg0==0的时辰默示什么都没做。

        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
            // TODO Auto-generated method stub
            // 当页面在滑动的时候会调用此方法，在滑动被停止之前，
            // arg0 :当前页面，及你点击滑动的页面
            // arg1:当前页面偏移的百分比
            // arg2:当前页面偏移的像素位置
        }

        @Override
        public void onPageSelected(int arg0) {
            // TODO Auto-generated method stub
            // 此方法是页面跳转完后得到调用，arg0是你当前选中的页面的Position

            tv_title1.setTextColor(Color.parseColor("#000000"));
            tv_title2.setTextColor(Color.parseColor("#000000"));
            tv_title3.setTextColor(Color.parseColor("#000000"));
            tv_title4.setTextColor(Color.parseColor("#000000"));

            if (arg0 == 0) {
                tv_title1.setTextColor(Color.parseColor("#3F51B5"));
            } else if (arg0 == 1) {
                tv_title2.setTextColor(Color.parseColor("#3F51B5"));
            } else if (arg0 == 2) {
                tv_title3.setTextColor(Color.parseColor("#3F51B5"));
            } else if(arg0 == 3) {
                tv_title4.setTextColor(Color.parseColor("#3F51B5"));
            }
            Animation animation = new TranslateAnimation(nowleft, screenW
                    * arg0, 0, 0);// 显然这个比较简洁，只有一行代码。
            animation.setFillAfter(true);// True:图片停在动画结束位置
            animation.setDuration(100);
            animation.setRepeatMode(Animation.REVERSE);// 设置反方向执行
            bg_move.startAnimation(animation);
            nowleft = screenW * arg0;

        }

    }
    @Override
    protected void onStart() {
        super.onStart();
        Log.e(Tga, "onStart");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.e(Tga, "onPause");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.e(Tga, "onResume");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.e(Tga, "onStop");
    }
}
